package com.wjh.graduation.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.wjh.graduation.domain.*;
import com.wjh.graduation.service.BillService;
import com.wjh.graduation.util.Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import javax.validation.constraints.NotNull;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Wu Jiahong
 * @since 2022-02-03
 */
@RestController
@RequestMapping("/bill")
@Api(tags = "账单有关接口")
public class BillController {
	@Autowired
	BillService billService;
	@Autowired
	Util util;

	@PostMapping("/income")
	@ApiOperation("收入记账")
	public R income(@RequestBody @Validated @ApiParam("账单时间、类型，金额、用户Id为必填项") Bill bill) {
		//1代表收入
		bill.setType(1);
		if (billService.save(bill)) {
			return new R(200, "添加成功");
		} else {
			return new R(303, "添加失败");
		}
	}


	@PostMapping("/expend")
	@ApiOperation("支出记账")
	public R expend(@RequestBody @Validated @ApiParam("账单时间、类型、金额、用户Id为必填项") Bill bill) {
		//0代表收入
		bill.setType(0);
		if (billService.save(bill)) {
			return new R(200, "添加成功");
		} else {
			return new R(303, "添加失败");
		}
	}

	@GetMapping("/getT")
	@ApiOperation("通过月份获得月支出、收入总数")
	public R getT(@NotNull @RequestParam @ApiParam("传入账单时间，格式2022-1") @JsonFormat(pattern = "yyyy-MM") Date beginTime, @NotNull @RequestParam @ApiParam("用户ID") Integer userId) {
		//获得月末时间
		String end = util.dateToString(util.lastDay(beginTime));
		String begin = util.dateToString(beginTime);
		//条件
		QueryWrapper<Bill> queryWrapper0 = new QueryWrapper<>();
		queryWrapper0.select("money").eq("user_id", userId).eq("type", 0).between("time", begin, end);
		QueryWrapper<Bill> queryWrapper1 = new QueryWrapper<>();
		queryWrapper1.select("money").eq("user_id", userId).eq("type", 1).between("time", begin, end);
		List<Bill> list0 = billService.list(queryWrapper0);
		List<Bill> list1 = billService.list(queryWrapper1);
		List<Double> list = new ArrayList<>();
		list.add(list0.stream().mapToDouble(Bill::getMoney).sum());
		list.add(list1.stream().mapToDouble(Bill::getMoney).sum());
		return new R(200, list, "获取成功");
	}


	@GetMapping("/getDayBill")
	@ApiOperation("通过月份获得每日支出详情")
	public R<List<DayBill>> getDayBill(@NotNull @RequestParam @ApiParam("传入月份时间，格式2022-1") @JsonFormat(pattern = "yyyy-MM") Date beginTime, @NotNull @RequestParam @ApiParam("用户ID") Integer userId) {
		//获得月末时间
		Date endTime = util.lastDay(beginTime);
		//创建回传列表
		List<DayBill> list = new ArrayList<>();
		//创建改变时间变量
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(beginTime);
		//循环遍历一整个月时间
		for (Date b = beginTime; b.compareTo(endTime) != 1; calendar.add(Calendar.DAY_OF_MONTH, +1), b = calendar.getTime()) {
			//格式化时间
			String time = util.dateToString(b);
			DayBill dayBill = new DayBill();
			dayBill.setDate(time);
			//查询条件
			QueryWrapper<Bill> queryWrapper = new QueryWrapper<>();
			queryWrapper.select("bill_id", "category", "description", "money", "type").eq("user_id", userId).eq("time", time);
			List<Bill> billList = billService.list(queryWrapper);
			dayBill.setData(billList);
			//查询支出总和条件
			QueryWrapper<Bill> qw0 = new QueryWrapper<>();
			qw0.select("money").eq("user_id", userId).eq("time", time).eq("type", 0);
			List<Bill> billList0 = billService.list(qw0);
			double sum0 = billList0.stream().mapToDouble(Bill::getMoney).sum();
			dayBill.setOutTotal(sum0);
			//查询收入总和条件
			QueryWrapper<Bill> qw1 = new QueryWrapper<>();
			qw1.select("money").eq("user_id", userId).eq("time", time).eq("type", 1);
			List<Bill> billList1 = billService.list(qw1);
			double sum1 = billList1.stream().mapToDouble(Bill::getMoney).sum();
			dayBill.setInTotal(sum1);
			//把dayBill加到返回的列表中
			if (dayBill.getInTotal() != 0 || dayBill.getOutTotal() != 0) {
				list.add(dayBill);
			}
		}
		return new R<List<DayBill>>(200, list, "获取成功");
	}

	@GetMapping("/getInPieChart")
	@ApiOperation("获得收入饼状图")
	public R<List<PieChart>> getInPieChart(@NotNull @RequestParam @ApiParam("传入月份时间，格式2022-1") @JsonFormat(pattern = "yyyy-MM") Date beginTime, @NotNull @RequestParam @ApiParam("用户ID") Integer userId) {
		//获得月末时间
		Date endTime = util.lastDay(beginTime);
		//时间格式化
		String begin = util.dateToString(beginTime);
		String end = util.dateToString(endTime);
		//创建返回的列表
		List<PieChart> list = new ArrayList<>();
		//条件
		QueryWrapper<Bill> qw1 = new QueryWrapper<>();
		qw1.eq("user_id", userId).eq("type", 1).eq("category", "工资").between("time", begin, end);
		QueryWrapper<Bill> qw2 = new QueryWrapper<>();
		qw2.eq("user_id", userId).eq("type", 1).eq("category", "兼职").between("time", begin, end);
		QueryWrapper<Bill> qw3 = new QueryWrapper<>();
		qw3.eq("user_id", userId).eq("type", 1).eq("category", "理财").between("time", begin, end);
		QueryWrapper<Bill> qw4 = new QueryWrapper<>();
		qw4.eq("user_id", userId).eq("type", 1).eq("category", "其他").between("time", begin, end);
		list.add(new PieChart("工资", billService.list(qw1).stream().mapToDouble(Bill::getMoney).sum()));
		list.add(new PieChart("兼职", billService.list(qw2).stream().mapToDouble(Bill::getMoney).sum()));
		list.add(new PieChart("理财", billService.list(qw3).stream().mapToDouble(Bill::getMoney).sum()));
		list.add(new PieChart("其他", billService.list(qw4).stream().mapToDouble(Bill::getMoney).sum()));
		return new R<List<PieChart>>(200, list, "获取成功");
	}


	@GetMapping("/getOutPieChart")
	@ApiOperation("获得支出饼状图")
	public R<List<PieChart>> getOutPieChart(@NotNull @RequestParam @ApiParam("传入月份时间，格式2022-1") @JsonFormat(pattern = "yyyy-MM") Date beginTime, @NotNull @RequestParam @ApiParam("用户ID") Integer userId) {
		//获得月末时间
		Date endTime = util.lastDay(beginTime);
		//时间格式化
		String begin = util.dateToString(beginTime);
		String end = util.dateToString(endTime);
		//创建返回的列表
		List<PieChart> list = new ArrayList<>();
		//条件
		QueryWrapper<Bill> qw1 = new QueryWrapper<>();
		qw1.select("money").eq("user_id", userId).eq("type", 0).eq("category", "餐饮").between("time", begin, end);
		QueryWrapper<Bill> qw2 = new QueryWrapper<>();
		qw2.select("money").eq("user_id", userId).eq("type", 0).eq("category", "住房").between("time", begin, end);
		QueryWrapper<Bill> qw3 = new QueryWrapper<>();
		qw3.select("money").eq("user_id", userId).eq("type", 0).eq("category", "通讯").between("time", begin, end);
		QueryWrapper<Bill> qw4 = new QueryWrapper<>();
		qw4.select("money").eq("user_id", userId).eq("type", 0).eq("category", "娱乐").between("time", begin, end);
		QueryWrapper<Bill> qw5 = new QueryWrapper<>();
		qw5.select("money").eq("user_id", userId).eq("type", 0).eq("category", "交通").between("time", begin, end);
		QueryWrapper<Bill> qw6 = new QueryWrapper<>();
		qw6.select("money").eq("user_id", userId).eq("type", 0).eq("category", "其他").between("time", begin, end);
		list.add(new PieChart("餐饮", billService.list(qw1).stream().mapToDouble(Bill::getMoney).sum()));
		list.add(new PieChart("住房", billService.list(qw2).stream().mapToDouble(Bill::getMoney).sum()));
		list.add(new PieChart("通讯", billService.list(qw3).stream().mapToDouble(Bill::getMoney).sum()));
		list.add(new PieChart("娱乐", billService.list(qw4).stream().mapToDouble(Bill::getMoney).sum()));
		list.add(new PieChart("交通", billService.list(qw5).stream().mapToDouble(Bill::getMoney).sum()));
		list.add(new PieChart("其他", billService.list(qw6).stream().mapToDouble(Bill::getMoney).sum()));
		return new R<List<PieChart>>(200, list, "获取成功");
	}


	@ApiOperation("删除账单")
	@GetMapping("/delete")
	public R deleteBill(@ApiParam("传入账单的的ID") @RequestParam Integer id) {
		if (billService.removeById(id)) {
			return new R(200, "删除成功");
		} else {
			return new R(303, "删除失败");
		}
	}

	@ApiOperation("修改账单")
	@PostMapping("/update")
	public R updateBill(@Validated @ApiParam(name = "传入金额、账单时间、类目、用户ID为必填项") @RequestBody Bill bill) {
		if (billService.updateById(bill)) {
			return new R(200, "修改成功");
		} else {
			return new R(303, "修改失败");
		}
	}


	@InitBinder
	public void initBinder(WebDataBinder binder, WebRequest request) {
		//转换日期 注意这里的转化要和传进来的字符串的格式一直 如2015-9-9 就应该为yyyy-MM-dd
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
		// CustomDateEditor为自定义日期编辑器
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
}

